const {Tool, API, Event, Storage} = global.$util

Page({
    data: {
        addressType: 1,
        addressList: []
    },
    onLoad: function (options) {
        Tool.isIPhoneX(this)
        this.queryUserAddressList()
        Event.on('updateAddressList', this.queryUserAddressList, this)
        this.data.door = options.door || ''
        this.data.addressId = options.id || ''
    },
    onShow: function () {

    },
    // 一键获取微信地址
    getWxAddress() {
        let _this = this
        wx.chooseAddress({
            success (res) {
                let params = {
                    receiver: res.userName,
                    receiverPhone: res.telNumber,
                    address: res.detailInfo,
                    provinceCode: res.nationalCode.toString().slice(0, 2) + '0000000',
                    cityCode: res.nationalCode.toString().slice(0, 4) + '00000',
                    areaCode: res.nationalCode + '000',
                    streetCode:'',
                }
                _this.saveUserAddress(params)
            }
          })
    },
    saveUserAddress(params) { // 添加地址
        params = {
            ...params,
            defaultStatus: 2,
            id: ''
        }
        API.addUserAddress(params).then((res) => {
            let tips = '添加成功'
            Event.emit('updateAddressList');//发出通知
            Tool.showSuccessToast(tips, ()=>{
                this.loading = false
            })
        }).catch((res) => {
            this.loading = false
            console.log(res)
        })
    },
    addressClicked(e) {
        if (this.data.door == 1) {
            let index = e.currentTarget.dataset.index
            Storage.setOrderAddress(this.data.addressList[index])
            Event.emit('updateOrderAddress')
            Tool.navigationPop()
        }
    },
    queryUserAddressList() {
        API.queryUserAddressList({}).then((res) => {
            let data = res.data || []
            data.forEach((item) => {
                item.addressInfo = item.province + item.city + item.area + (item.street || '') + item.address
                item.hasData = true
            })
            if (data.length > 0) {
                this.setData({
                    addressList: data
                })
            }
        }).catch((res) => {
            console.log(res)
        });
    },
    setDefault(e) {
        API.setDefaultAddress({
            id: this.getAddressId(e).id,
        }).then((res) => {
            this.queryUserAddressList()
        }).catch((res) => {
            console.log(res)
        });
    },
    getAddressId(e) {
        let index = e.currentTarget.dataset.index
        let id = this.data.addressList[index].id
        return {index, id}
    },
    deleteAddress(e) {
        let item = this.getAddressId(e)
        let callBack = () => {
            API.deleteAddress({
                id: item.id,
            }).then((res) => {
                let list = this.data.addressList
                list.splice(item.index, 1)
                this.setData({
                    addressList: list
                })
                // 订单页面来的时候 删除了订单确认页面的地址 并且没有选择其他地址 直接点返回的情况下 做处理
                if (this.data.door == 1 && item.id == this.data.addressId) {
                    Storage.setOrderAddress({})
                    Event.emit('updateOrderAddress')
                }
            }).catch((res) => {
                console.log(res)
            });
        }
        Tool.showComfirm('是否确认删除此地址', callBack)
    },
    editAddress(e) {
        let index = this.getAddressId(e).index
        let list = this.data.addressList[index]
        this.newAddress(list, 1)
    },
    newAddress(list, types) {
        let page = '/pages/address/new-address/new-address'
        if (types == 1) {
            page = page + '?address=' + JSON.stringify(list)
        } else {
            if (this.data.addressList.length >= 20) {
                Tool.showToast('收货地址已达上限,请删除部分后再试')
                return
            }
        }
        Tool.navigateTo(page)
    },
    onUnload: function () {
        Event.off('updateAddressList', this.queryUserAddressList);
    },
})
